<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
   class Content_manage extends Admin_Controller
   {
	       public function __construct()
	      {
	         parent::__construct();
	         $this->load->model('Category_model');
	         $this->load->model('Product_model');
	      }
	      function toggle_url()
		  {
	              $id=$this->input->post('id',TRUE);      	
		      	  $res=$this->Category_model->get_by('id',$id);
		      	  if(!empty($res))
		      	  {
		      	  	 $static = $res['statics'] ? '0':'1';
		      	  	 $data=array('statics'=>$static);
		      	  	 $data=$this->Category_model->update($id,$data);
		      	  	  if($data)
		      	  	 echo 1;//更改开启状态
		      	  	  else
		      	  	 echo 0;
		      	  }
		    }
	      
          public function category_manage()
	      {
	      	$res['data']=$this->Category_model->get_all();
	      	           
	        foreach($res['data'] as $k=>$val)
	      	  {
	      	  	if($val['statics']=='0')
	      	 	 $res['data'][$k]['toggle_static']="开启";
	      	  	else
	      	  	 $res['data'][$k]['toggle_static']="禁用";
	      	  };
	      	$this->load->view('admin/category_manage',$res);
	      }
      
          function purview_up()
	      {
	      	$id=$this->input->post('id',TRUE);
	      	$up_data=$this->Category_model->getfield_by('id',$id);
	      	if(!$up_data)
	      	{
	      		 echo '1'; exit;//此栏目不存在.
	      	}
	      	$arr_pid=array();
	      	//组装二维数组
	      	$arr_arr=array();  
	      	$all_date=$this->Category_model->post(NULL,TRUE);
	      	foreach($all_date as $k=>$val)
	      	{
	      		if($up_data['pid']==$val['pid'])
	      		{
	      	         $arr_arr[]=$val;
	      		}
	      	}
	   
	      //目标数组key
	      	$arr_key='';
	      	$new_arr=array();
	      	$target_id='';
	      	$target_arr=array();
      		foreach($arr_arr as $k=>$val)
      		{
      			if($up_data['id']==$val['id'])
      			{
      				$arr_key=$k-1;
      				if($arr_key<0)
      				{
      					echo "2"; exit; //修改失败,已经在最顶端
      				}
      		        $new_arr=$val;
      			}
      		}
	        foreach($arr_arr as $k=>$val)
      		{
      	    	if($arr_key==$k)
      		 	{
      		 		$target_id=$val['id'];
      				$target_arr=$val;
      		 	}
      		}
	        $new_path=$this->Category_model->get_path($new_arr['path']);
	        $target_path=$this->Category_model->get_path($target_arr['path']);
	         //源path数组
	         $s_path=explode(',',$new_arr['path']);
	         //目标path数组
             $t_path=explode(',',$target_arr['path']);
             //源path的最后一位
             foreach($new_path as $val)
             {
             	$s_art_arr=array();
             	if($val['path']==$new_arr['path'])
             	{
             		$left_new_arr=array_splice($val, 4);
             		$this->Category_model->get_update($target_arr['id'],$left_new_arr);
             		$new_affect=$this->db->affected_rows();
             	}else
             	{
             		$s_art_path=explode(',',$val['path']);
        			$s_path_num=count($t_path);
					$s_left_path=array_splice($s_art_path,$s_path_num);
					$new_s_str_path= $target_arr['path'].','.implode(',',$s_left_path);
					$s_depth=$new_arr['depth']+1;
             		if($s_depth==$val['depth'])
             		{
             			$s_art_arr['pid']=$target_id;
             		}
             		$s_art_arr['path']=$new_s_str_path;
             		$this->Category_model->get_update($val['id'],$s_art_arr);
             	}          
             }
	         foreach($target_path as $val)
             {
             	$t_art_arr=array();
             	if($val['path']==$target_arr['path'])
             	{
             		$left_target_arr=array_splice($val, 4);
             		$this->Category_model->get_update($new_arr['id'],$left_target_arr);
             		$target_affect=$this->db->affected_rows();
             	}else
             	{
             		$t_art_path=explode(',',$val['path']);
        			$t_path_num=count($s_path);
					$t_left_path=array_splice($t_art_path,$t_path_num);
					$new_t_str_path= $new_arr['path'].','.implode(',',$t_left_path);
					$t_depth=$target_arr['depth']+1;
             		if($t_depth==$val['depth'])
             		{
             		   $t_art_arr['pid']=$up_data['id'];	
             		}
             		$t_art_arr['path']=$new_t_str_path;
             		$this->Category_model->get_update($val['id'],$t_art_arr);
             	}            	
             }
             if($target_affect && $new_affect)
             {
             	echo "3";exit;
             }
	      }
	      function purview_down()
	      {
		        $id=$this->input->post('id',TRUE);
		      	$up_data=$this->Category_model->getfield_by('id',$id);
		      	if(!$up_data)
		      	{
		      		 echo '1'; exit;//此栏目不存在.
		      	}
		      	$arr_pid=array();
		      	//组装二维数组
		      	$arr_arr=array();  
		      	$all_date=$this->Category_model->post(NULL,TRUE);
		      	foreach($all_date as $k=>$val)
		      	{
		      		if($up_data['pid']==$val['pid'])
		      		{
		      	         $arr_arr[]=$val;
		      		}
		      	}
		   
		      //目标数组key
		      	$arr_key='';
		      	$new_arr=array();
		      	$target_id='';
		      	$target_arr=array();
	      		foreach($arr_arr as $k=>$val)
	      		{
	      			if($up_data['id']==$val['id'])
	      			{
	      				$arr_key=$k+1;
	      				if($arr_key>count($arr_arr)-1)
	      				{
	      					echo "2"; exit; //修改失败,已经在最底端
	      				}
	      		        $new_arr=$val;
	      			}
	      		}
		        foreach($arr_arr as $k=>$val)
	      		{
	      	    	if($arr_key==$k)
	      		 	{
	      		 		$target_id=$val['id'];
	      				$target_arr=$val;
	      		 	}
	      		}
		        $new_path=$this->Category_model->get_path($new_arr['path']);
		        $target_path=$this->Category_model->get_path($target_arr['path']);
		         //源path数组
		         $s_path=explode(',',$new_arr['path']);
		         //目标path数组
	             $t_path=explode(',',$target_arr['path']);
	             //源path的最后一位
	             foreach($new_path as $val)
	             {
	             	$s_art_arr=array();
	             	if($val['path']==$new_arr['path'])
	             	{
	             		$left_new_arr=array_splice($val, 4);
	             		$this->Category_model->get_update($target_arr['id'],$left_new_arr);
	             		$new_affect=$this->db->affected_rows();
	             	}else
	             	{
	             		$s_art_path=explode(',',$val['path']);
	        			$s_path_num=count($t_path);
						$s_left_path=array_splice($s_art_path,$s_path_num);
						$new_s_str_path= $target_arr['path'].','.implode(',',$s_left_path);
						$s_depth=$new_arr['depth']+1;
	             		if($s_depth==$val['depth'])
	             		{
	             			$s_art_arr['pid']=$target_id;
	             		}
	             		$s_art_arr['path']=$new_s_str_path;
	             		$this->Category_model->get_update($val['id'],$s_art_arr);
	             	}          
	             }
		         foreach($target_path as $val)
	             {
	             	$t_art_arr=array();
	             	if($val['path']==$target_arr['path'])
	             	{
	             		$left_target_arr=array_splice($val, 4);
	             		$this->Category_model->get_update($new_arr['id'],$left_target_arr);
	             		$target_affect=$this->db->affected_rows();
	             	}else
	             	{
	             		$t_art_path=explode(',',$val['path']);
	        			$t_path_num=count($s_path);
						$t_left_path=array_splice($t_art_path,$t_path_num);
						$new_t_str_path= $new_arr['path'].','.implode(',',$t_left_path);
						$t_depth=$target_arr['depth']+1;
	             		if($t_depth==$val['depth'])
	             		{
	             		   $t_art_arr['pid']=$up_data['id'];	
	             		}
	             		$t_art_arr['path']=$new_t_str_path;
	             		$this->Category_model->get_update($val['id'],$t_art_arr);
	             	}            	
	             }
	             if($target_affect && $new_affect)
	             {
	             	echo "3";exit;
	             }
	      }
      
      public function artice_model()
      {
      	
      	
      }
      public function prodect_model()
      {
      	 
          $data['data']=$this->Product_model->order_by("sort", "asc")
                                            ->get_all();
          $this->load->view('admin/prodect_model',$data);
      }
      
      public function pro_sort()
      {
      	 $sort=$this->input->post('nSort',TRUE);
      	 $id=$this->input->post('id',TRUE);
      	 $this->Product_model->get_all();
         $new_array= array();
		  if($sort)
		  {
		      foreach($sort as $k=>$v)
			  {
			  	$new_array[$k]['sort']= $v;
			  }
		  }
		 
		  if($id)
		  {
		      foreach($id as $k=>$v)
			  {
			  	$new_array[$k]['id']= $v;
			  }
		  }
      	  $this->Product_model->sort_updata($new_array);
		  $link[0]['href'] = site_aurl('content_manage/prodect_model');
          $link[0]['text'] = "返回产品列表";
          $this->common->adminmsg('排序成功','2',$link);
      }
      
      public function del_product()
      {
      	  $add_check=$this->input->post('add_check',TRUE);
      	  $val=implode(',',$add_check);      	
      	  $data=$this->Product_model->del_product($val);
          if($data)
      	      {
                	$link[0]['href'] = site_aurl('content_manage/prodect_model');
                	$link[0]['text'] = "返回产品列表";
                	$this->common->adminmsg('操作成功','2',$link);
      	  	  }else 
      	  	  {
      	  	  	    $link[0]['text'] = "返回产品列表";
                	$link[0]['href'] = site_aurl('content_manage/prodect_model');
                	$this->common->adminmsg('操作失败','1',$link);
      	  	  } 
      }
      
      
      public function delSingle()
      {
      	  $id=$this->uri->segment(4);
      	  $data=$this->Product_model->delSingle($id);
          if($data)
          {
                $link[0]['href'] = site_aurl('content_manage/prodect_model');
                $link[0]['text'] = "返回产品列表";
                $this->common->adminmsg('操作成功','2',$link);
          }else 
          {
                $link[0]['text'] = "返回产品列表";
                $link[0]['href'] = site_aurl('content_manage/prodect_model');
                $this->common->adminmsg('操作失败','1',$link);
          } 
      }
      
      
      public function add_product()
      {
      	  if($this->form_validation->run('add_product') == FALSE) 
      	  {
      	  	  $this->load->view('admin/add_product');
      	  }else
      	  {
      	  	 $POST=$this->input->post(NULL,TRUE);
      	  	 $time=time();
      	  	 $data=array('title'=>$POST['title'],'content'=>$POST['content1'],'sort'=>$POST['sort'],'createtime'=>$time);
      	     $this->Product_model->insert($data);
      	     
      	      if($this->db->affected_rows()>0)
      	      {
                	$link[0]['href'] = site_aurl('content_manage/prodect_model');
                	$link[0]['text'] = "返回产品列表";
                	$link[1]['href'] = site_aurl('content_manage/add_product');
                	$link[1]['text'] = "添加产品";
                	$this->common->adminmsg('操作成功','2',$link);
      	  	  }else 
      	  	  {
      	  	  	    $link[0]['text'] = "添加失败";
                	$link[0]['href'] = site_aurl('content_manage/add_product');
                	$this->common->adminmsg('操作失败','1',$link);
      	  	  }
      	  }
      }
      
      public function image_model()
      {
      	
      	
      }
      public function leave_model()
      {
      	
      	
      }
      public function down_model()
      {
      	
      	
      }
      // 添加栏目
       public function ajax_add_cate()
       {
       	   $post=$this->input->post(NULL,TRUE);
       	   $url='content_manage/'.$post['cate_dir'];
       	   $arr_data=array('model'=>$post['model_select'],'pid'=>$post['cate_select'],'url'=>$url,'title'=>$post['cate_name'],'statics'=>$post['navigation']);
       	   $insert_data=$this->Category_model->insert($arr_data);
       	   $new_id=$this->db->insert_id();
       	   $data=$this->Category_model->getfield_by('id',$post['cate_select']);
           if(!empty($data))
           {
           	    $new_path=$data['path'].',$'.$new_id.'$';
           }else
           {
            	$new_path='$'.$new_id.'$';
           }
       	   $new_depth=count(explode(',',$new_path))-1;
       	   $arr_update=array('depth'=>$new_depth,'path'=>$new_path);
       	   $this->Category_model->update($new_id,$arr_update);
       	   if($this->db->affected_rows()>0)
       	   {
              echo 1;//数据插入成功
       	   }else
       	   {
       	   	  echo 0;//数据插入失败
       	   }
       }
       // 编辑栏目
       public function ajax_edit_cate()
       {
       	   $id=$this->input->post('id',TRUE);
       	   $data=$this->Category_model->getfield_by('id',$id);
       	   $url=explode('/',$data['url']);
       	   $data['dir']=$url['1'];
       	   echo json_encode($data);
       }
       //删除栏目
       public function del_category()
       {
       	 $id=$this->uri->segment(4);
       	 $this->Category_model->delete($id);
       	 if($this->db->affected_rows()>0)
       	 {
       	 	header("Location:" . $_SERVER['HTTP_REFERER']);
       	 }else
       	 {
       	 	show_error("删除失败");
       	 }
       	
       	
       }
       //保存栏目
        public function ajax_save_cate()
       {
       	  $post=$this->input->post(NULL,TRUE);
       	  $id=$post['cate_select'];
       	  $data=$this->Category_model->getfield_by('id',$id);
       	  if(!empty($data))
       	  $new_path=$data['path'].',$'.$post['id'].'$';
       	  else
       	  $new_path='$'.$post['id'].'$';
       	  $new_depth=count(explode(',',$new_path))-1;
       	  $arr_data=
       	  array(
       	  'model'=>$post['model_select'], 
       	  'statics'=>$post['navigation'],
       	  'pid'=>$post['cate_select'],
       	  'depth'=>$new_depth,
       	  'path'=>$new_path,
       	  );
       	  $this->Category_model->update($post['id'],$arr_data);
       	  if($this->db->affected_rows()>0)
       	  	echo 1;
       	  else
       	  	echo 0;
       }
   }
?>   